<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;

use app\model\Cleaning;
use app\model\CleaningCheck;
use app\model\CleaningPoint;
use app\model\SignIn;
use app\model\User;
use think\facade\Request;

class CleaningCheckLogic
{
    /*
     * 提交巡检
     */
    public function check(){

        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];
        $cleaningCheckModel = new CleaningCheck();

        if(!(position(User::CLEANING_HEAD)) || position(User::CLEANING_CHARGE))  fail('巡检失败，暂无权限');
        $param = Request::post();

        $ck = [
            'ck_orgid' => $orgId,
            'ck_check_uid' => $uid,
            'ck_check_name' => config('user')['pername'],
        ];

        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $ck['ck_cleaning_point_id'] = intval($param['cleaningPointId']);

        if(!isset($param['cleaningId']) || empty($param['cleaningId']))   fail('暂无打点信息，不能巡检');
        $ck['ck_cleaning_id'] = intval($param['cleaningId']);

//        //判断是否巡检过了
//        $ckInfo = $cleaningCheckModel->getInfoByWhere(['ck_cleaning_id'=>$ck['ck_cleaning_id']]);
//        if($ckInfo) fail('不能重复巡检');

        if(!isset($param['star']) || empty($param['star']))   fail('请给保洁员评星');
        $ck['ck_star'] = intval($param['star']);

        if(!isset($param['finish']) || empty($param['finish']))   fail('请选择完成状态');
        $ck['ck_finish'] = intval($param['finish']);

        if(!isset($param['attach']) || empty($param['attach']))   fail('请选择附件');
        $ck['ck_attach'] = trim($param['attach']);

        if(!isset($param['remark']) || empty($param['remark']))   fail('请输入说明');
        $ck['ck_remark'] = trim($param['remark']);

        $rt = $cleaningCheckModel->addOneCheck($ck);
        if(!$rt)    fail('打点失败，请稍后重试');
        return true;

    }

    public function getCheckInfo(){

        $param = Request::get();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $cleaningPointId = intval($param['cleaningPointId']);
        $cleaningModel = new Cleaning();

        $pointModel = new CleaningPoint();
        $pointInfo = $pointModel->getPointInfoById($cleaningPointId);
        if(!$pointInfo) fail('保洁点信息不存在');
        $info = $cleaningModel->getTodayLastInfoByPointId($cleaningPointId);
        if(!$info){
            $info = [
                'cleaningId' => 0,
                'pername' => '',
                'cleaningTime' => 0,
                'cleaningPointId' => $pointInfo['cleaningPointId'],
                'cleaningPointName' => $pointInfo['cleaningPointName'],
            ];
        }
        return $info ? $info : $pointInfo;
    }
}